
ENTRYPOINT	= 0x0

# Offset of entry point in kernel file
# It depends on ENTRYPOINT
ENTRYOFFSET	=   0x0


# Programs, flags, etc.
CC		= gcc -march=i486
LD		= ld
CPP		= gcc -E -nostdinc -I include
CFLAGS		= -Wall  -fno-stack-protector -finline-functions -nostdinc -I include
LDFLAGS		= -m elf_i386 -Ttext $(ENTRYPOINT) -e $(ENTRYOFFSET)

# 若无-O参数, 则extern inline函数就会报错, 但是尽管如此我们也最好不要使用-O参数, -O带来的优化可能使
# 代码发生错误........
# This Program
ARCHIVES = boot/head.o kernel/kernel.o
DRIVERS	 = drivers/drivers.o
SERVICES = servers/servers.o
LIBS	 = lib/lib.a

# All Phony Targets
.PHONY : Image clean all dep backup run debug

# Default starting position
all : zImage

zImage: boot/bootsect boot/setup tools/system tools/build
	tools/build boot/bootsect boot/setup tools/system > zImage
	sync

install: ../bochs/fd_minix_grub.img

../bochs/fd_minix_grub.img : zImage
	sudo mount ../bochs/fd_minix_grub.img ../mnt -t minix -o loop
	sudo cp -f zImage ../mnt
	sudo umount ../mnt

backup : clean_target
	(cd ..; tar zcvf cos.tar.gz src > cos.tar.gz.map)
	
run :	install
	(cd ../bochs; ./bochs -q &)

runexam:
	(cd ../bochs; bochs-bin -q -f hdbochs.txt)
debug :	install 
	(cd ../bochs; ./bochs-debug -q)
xdebug:
	(cd ../bochs; bfe2)

tools/system : $(ARCHIVES) $(DRIVERS) $(SERVICES) $(LIBS)
	$(LD) $(LDFLAGS) \
	$(ARCHIVES) \
	$(DRIVERS) \
	$(SERVICES) \
	$(LIBS) \
	-o tools/system
	nm tools/system | grep -v '\(compiled\)\|\(\.o$$\)\|\([aU]\)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)'| sort > tools/System.map

boot/bootsect : boot/bootsect.S
	(cd boot; make bootsect)

boot/setup : boot/setup.S
	(cd boot; make setup)

boot/head.o : boot/head.S
	(cd boot; make)

kernel/kernel.o :
	(cd kernel; make)

drivers/drivers.o :
	(cd drivers; make)

servers/servers.o :
	(cd servers; make)

lib/lib.a:
	(cd lib; make)

tools/build : tools/build.c
	(cd tools; make)

clean:
	rm -f boot/*.o tmp_make
	(cd boot; make clean)
	(cd kernel; make clean)
	(cd drivers; make clean)
	(cd servers; make clean)
	(cd lib; make clean)
	(cd tools; make clean)

clean_target:
	rm -f boot/*.o tmp_make
	(cd kernel; make clean)
	(cd drivers; make clean)
	(cd servers; make clean)
	(cd lib; make clean)
dep:
	(cd kernel; make dep)
	(cd drivers; make dep)
	(cd servers; make dep)
	(cd lib; make dep)

### Dependencies:
